﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GPMS.Web.Controllers
{
    using GPMS.Web.Helpers;
    using GPMS.Model;
using GPMS.BLL;
    /// <summary>
    /// 用户控制器
    /// </summary>
    public class AccountController : BaseController
    {
        /// <summary>
        /// 用户主页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {

            throw new NotImplementedException();
        }

        public ActionResult List()
        {
            ViewData["Specialty"] = gpmsDb.Specialties.ToList();
            return View(AccountHelper.GetList(gpmsDb));
        }

        public ActionResult Edit(Guid? id)
        {
            ViewData["Specialty"] = gpmsDb.Specialties.ToList();
            Guid userid = id ?? AccountHelper.GetLoginUserID(Request);
            ViewData["useredit"] = id;
            return View(AccountHelper.Get(userid, gpmsDb));
        }
        
        [HttpPost]
        public ActionResult Edit(Guid? id, FormCollection collection)
        {
            try
            {
                Guid userid = id ?? AccountHelper.GetLoginUserID(Request);
                UserInfo user = gpmsDb.UserInfos.FirstOrDefault(u => u.UserID == userid);
                UpdateModel(user, collection);
                gpmsDb.SaveChanges();
                return Success(null);
            }
            catch (Exception ex)
            {
                return JsonError("更新失败:" + ex.Message);
            }
            
        }

        public ActionResult SetRole(Guid id)
        {
            return View(AccountHelper.GetUserRole(id,gpmsDb));
        }
        [HttpPost]
        public ActionResult SetRole(Guid id, FormCollection collection)
        {
            try
            {
                AccountHelper.SaveUserRole(id, collection, gpmsDb);
                return Success(null);
            }
            catch (Exception e)
            {
                return JsonError("更新失败:" + e.Message);
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        public ActionResult Register()
        {
            AccountVM model = AccountHelper.GetAccountVM(gpmsDb);
            return PartialView(model);
        }

        /// <summary>
        /// 注册处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Register(UserInfo user)
        {
            if (ModelState.IsValid)
            {
                AccountHelper.Add(user,gpmsDb);
                return Success("注册成功");
            }
            else
            {
                return JsonError(GetError(ModelState));
            }
        }

      
  
        public ActionResult Login()
        {
            return PartialView(new LoginVM());
        }

        [HttpPost]
        public ActionResult Login(LoginVM model)
        {
            if (ModelState.IsValid)
            {
                if (AccountHelper.SignIn(model, Response,gpmsDb))
                {
                    return Success("登陆成功! 欢迎您， "+AccountHelper.GetUserRoleStr(Request,gpmsDb));
                }
               
            }
            return JsonError("登陆失败，请检查用户名和密码");
        }

        public JsonResult IsLogin()
        {
            bool result = AccountHelper.IsLogin(HttpContext);
            return Json(result);
        }

        public ActionResult Logout()
        {
            AccountHelper.SignOut();
            return RedirectToAction("Index", "Home");
        }
    }
}
